import { GameConfig } from '../Config/GameConfig';
import { Singleton } from '../Manager/BaseManager';
import { HandleCode } from './Handler';
import { HandlerManager } from './HandlerManager';
/* eslint-disable */
class WebSocketManger extends Singleton {
  className: string = 'WebSocketManger';
  //主链接
  socket: WebSocket = null!;
  private _hear: HeartBeat = null!;
  //跨服链接
  otherSocket: WebSocket = null!;

  connerctToServer(serverUrl: string) {
    if (this.socket) {
      throw new Error('警告，正在重复链接服务器');
    }
    this.socket = new WebSocket(serverUrl);
    console.log('开始链接服务器：' + serverUrl);
    this.socket.binaryType = 'arraybuffer';
    this.socket.onopen = this.onSocketOpen;
    this.socket.onmessage = this.onSocketMessage;
    this.socket.onclose = this.onSocketClose;
    this.socket.onerror = this.onSocketError;

    this._hear = new HeartBeat();
  }

  private onSocketOpen(event: Event) {
    console.log('服务器链接成功');
    //登录
    HandlerManager.Ins().handle(HandleCode.MAIN_SIGNIN);
    //开始心跳
    this._hear.start();
  }

  private onSocketClose(event: CloseEvent) {
    console.log('服务器链接断开');
  }

  private onSocketError(event: Event) {
    console.log('服务器链接失败');
  }

  onSocketMessage(event: MessageEvent) {
    console.log('服务器收到消息', event.data);
    //TODO
    // SockerHandler.Ins().recvMsg(event.data);
  }

  sendWebSocketMsg(cmd: HandleCode, msg: any) {
    if (this.socket && this.socket.readyState === WebSocket.OPEN) {
      this.socket.send(msg);
    }
  }

  closeWebSocket() {
    if (this.socket) {
      this.socket.close();
    }
  }

  connerctToOtherServer(serverUrl: string) {
    if (this.otherSocket) {
      this.otherSocket.close();
    }

    this.otherSocket = new WebSocket(serverUrl);
    console.log('开始链接其他服务器');
    this.otherSocket.binaryType = 'arraybuffer';
    this.otherSocket.onopen = this.onOtherSocketOpen;
    this.otherSocket.onmessage = this.onOtherSocketMessage;
    this.otherSocket.onclose = this.onOtherSocketClose;
    this.otherSocket.onerror = this.onOtherSocketError;
  }

  private onOtherSocketOpen(event: Event) {
    console.log('跨服服务器链接成功');
  }

  sendOtherWebSocketMsg(cmd: HandleCode, msg: any) {}

  private onOtherSocketMessage(event: MessageEvent) {}

  private onOtherSocketClose(event: CloseEvent) {}

  private onOtherSocketError(event: Event) {
    console.log('跨服服务器链接失败');
  }
}

class HeartBeat {
  private readonly timeOut: number = 5000;
  private _timer: any = null!;
  private __severTimer: any = null!;

  start() {
    this.close();
    this._timer = setTimeout(() => {
      //发送心跳包
      this.__severTimer = setTimeout(() => {
        console.log('服务器心跳超时');
        //重连服务器
      }, 5000);
    }, this.timeOut);
  }

  close() {}
}

export class SockerHandler extends Singleton {
  className: string = 'SockerHandler';
  /** 链接服务器 */
  connectServer() {
    WebSocketManger.Ins().connerctToServer(GameConfig.serverUrl);
  }

  /** 请求服务器消息 */
  sendMsg(cmd: HandleCode, msg: any) {
    console.log('发送消息', cmd);
    WebSocketManger.Ins().sendWebSocketMsg(cmd, msg);
  }

  /** 接收服务器消息 */
  recvMsg(cmd: HandleCode, msg: any) {
    // const func = HandlerManager.Instantiate().handles.get(cmd);
    // if (func) throw new Error('没有注册的接受消息处理函数');
    // console.log('接收消息', cmd, msg);
    // func(msg);
    HandlerManager.Ins().handle(cmd, ...msg);
    console.log('接收消息', cmd);
  }
}
